package com.zaofan.service;

import com.zaofan.bean.Comment;
import com.zaofan.bean.Reply;
import com.zaofan.bean.ScoreAve;
import com.zaofan.dao.CommentDao;
import com.zaofan.exception.ServiceException;
import com.zaofan.utils.C3p0DataSourceUtils;
import com.zaofan.utils.SqlBuilderUtils;
import com.zaofan.utils.WebUtils;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommentService {
	private CommentDao commentDao = new CommentDao();
	private ReplyService replyService = new ReplyService();

	public void delete(Comment comment) {
		commentDao.del(comment);
	}

	public void add(Comment comment) {
		commentDao.add(comment);
	}

	public void update(Comment comment) {
		commentDao.update(comment);
	}

	public List<Comment> findAll(Integer cityId, Map<String, Object> params,String selectCityId) {
		if (cityId == 0&& "0".equals(selectCityId)) {
			return findAll(params);
		}
		QueryRunner qr = new QueryRunner(C3p0DataSourceUtils.getDataSource());
		List<Comment> data = null;
		try {
			String sql = "select comment.* from comment "
					+ " inner join orderinfo on orderinfo.orderNo = comment.orderNo "
					+ " where orderinfo.cityId= " + selectCityId
					+ " and orderinfo.parentId = 0 "
					+ " order " +  params.get("order")
					+ " limit " + params.get("begin") + "," + params.get("end");
			System.err.println(sql);
			data = qr.query(sql, new BeanListHandler<Comment>(Comment.class) );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}
	

	public Long count(Integer cityId, Map<String, Object> params,String selectCityId) {
		if (cityId == 0 &&  "0".equals(selectCityId)) {
			return count(params);
		}
		QueryRunner qr = new QueryRunner(C3p0DataSourceUtils.getDataSource());
		try {
			String sql = "select count(*) from comment "
					+ " inner join orderinfo on orderinfo.orderNo = comment.orderNo "
					+ " where orderinfo.cityId=" + selectCityId
					+ " and orderinfo.parentId = 0 ";
			System.err.println(sql);
			return qr.query(sql, new ScalarHandler<Long>());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}
	
	public List<Comment> findAll(Map<String, Object> params) {
		List<Comment> data = commentDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public Long count(Map<String, Object> params) {
		return commentDao.count(params);
	}

	public Comment findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return commentDao.findOne(params);
	}

	public List<Comment> findByShopId(Integer shopId) {
		return commentDao.findByShopId(shopId);
	}

	public Comment findByOrderId(int orderId) {
		Map<String, Object> params = new HashMap<>();
		params.put("orderId", orderId);
		return commentDao.findOne(params);
	}

	public void reply(String id_s, String content) throws ServiceException {
		Integer id = 0;
		try {
			id = Integer.valueOf(id_s);
		} catch (Exception e) {
			throw new ServiceException("评论信息错误!!");
		}
		if (WebUtils.isEmpty(content)) {
			throw new ServiceException("请输入回复内容!!");
		}
		Comment comment = this.findById(id);
		if (comment == null) {
			throw new ServiceException("评论信息错误!!");
		}
		if (comment.getIsReply() == 1) {
			throw new ServiceException("评论已回复!!");
		}
		Reply reply = new Reply();
		reply.setAddTime(new Date());
		reply.setCommentId(comment.getId());
		reply.setContent(content);
		comment.setIsReply(1);
		Connection connection = C3p0DataSourceUtils.getConnection();
		try {
			try {
				connection.setAutoCommit(false);
				QueryRunner runner = new QueryRunner();
				String sql = SqlBuilderUtils.builderInsertSql(Reply.class, reply);
				runner.update(connection, sql);
				sql = SqlBuilderUtils.builderUpdateSql(Comment.class, comment);
				runner.update(connection, sql);
				connection.commit();
			} catch (Exception e) {
				e.printStackTrace();
				connection.rollback();
				throw new ServiceException("回复失败!!");
			} finally {
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new ServiceException("回复失败!!");
		}
	}

	public void replyUpdate(String id_s, String content) throws ServiceException {
		Integer id = 0;
		try {
			id = Integer.valueOf(id_s);
		} catch (Exception e) {
			throw new ServiceException("评论信息错误!!");
		}
		if (WebUtils.isEmpty(content)) {
			throw new ServiceException("请输入回复内容!!");
		}
		Comment comment = this.findById(id);
		if (comment == null) {
			throw new ServiceException("评论信息错误!!");
		}
		if (comment.getIsReply() == 0) {
			throw new ServiceException("评论未回复!!");
		}
		Reply reply = replyService.findByCommentId(comment.getId());
		if (reply == null) {
			throw new ServiceException("评论未回复!!");
		}
		reply.setContent(content);
		replyService.update(reply);
	}

	public Reply getReply(Integer id) throws ServiceException {
		Reply reply = replyService.findByCommentId(id);
		if (reply == null) {
			throw new ServiceException("评论未回复!!");
		}
		return reply;
	}

	public ScoreAve avgScore(int shopId) {
		return commentDao.avgScore(shopId);
	}


	public List<Comment> findGoodsComments(int goodsId, int begin, int end) {
		return commentDao.findGoodsComments(goodsId,begin,end);
	}

	public List<Comment> findProductComments(int goodsId, int begin, int end) {
		return commentDao.findProductComments(goodsId,begin,end);
	}
}